'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to generate         ##
## monomial and binomial keys.                     ##
## monomial keys are used to store the distance    ##
## between two points. Binomial keys are used to   ##
## store the two points (start and end).           ##
## In literature Monomial and Binomial keys are    ##
## also refered to as private and public keys,     ##
## symmetric and asymmetric keys, decryption and   ##
## encryption keys.                                ##
#####################################################

#####################################################
##                   Pre-requisits                 ##
#####################################################
## The Xoron matrix decompressed data structure    ##
## needs to be mined before running this code      ##
## example. Some examples on how to mine Xoron     ##
## matrices can be found in                        ##
## generate_xrn_matrix_all_rnd.py or               ##
## generate_xrn_matrix_one_third.py                ##
#####################################################

import xrnlib256 as xrn

##########################
## Variable definitions ##
##########################

# create monomial key data structure
mk = xrn.xmonomial_key_t()

# create start point data structure and file pointer
sp = xrn.xstart_point_t()

# create binomial keyt data structure and file pointer
bk = xrn.xbinomial_key_t()

settings = xrn.xrn_crypto_extra_settings_t()
# all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt


#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  );

#############################
## Variable initialization ##
#############################

# Initialize default run time settings
# In this code example only the settings.rnd_str is used
# In the xrn_load_default_settings function xrn_make_monomial_key string is used.
# Other functions names can be also used to initialize settings

if ( xrn.XSUCCESS != xrn.xrn_load_default_settings(settings) ) :
    print("xrn_load_default_settings Failed")

###########################
## Make the xstart_point ##
###########################

print( "making start point ..." );

sp_fp = xrn.fopen("xstart_point.xf","w")
xm_fp = xrn.fopen("xrn_matrix.xm","r")
if (( xm_fp == None ) or ( sp_fp == None )) :
    print("Input Output Failed")

if ( xrn.XSUCCESS != xrn.xrn_make_start_point_wrapper( sp_fp, xm_fp, settings ) ) :
    print("Input Output Failed")
    xrn.fclose(sp_fp)
    xrn.fclose(xm_fp)
    exit( -1 )

xrn.fclose(sp_fp)
xrn.fclose(xm_fp)

#####################################
## Make xmonomial_key ( distance ) ##
#####################################

print( "making monomial key ..." );

mk_fp = xrn.fopen("xmonomial_key.xf","w")
if ( mk_fp == None ) :
    print("Input Output Failed")

if ( xrn.XSUCCESS != xrn.xrn_make_monomial_key_wrapper( mk_fp, settings ) ) :
    print("Input Output Failed")
    xrn.fclose(mk_fp)
    exit( -1 )

xrn.fclose(mk_fp)

#######################################
## Make xbinomial_key ( start, end ) ##
#######################################

print( "making binomial key ..." );

bk_fp = xrn.fopen("xbinomial_key.xf","w")
mk_fp = xrn.fopen("xmonomial_key.xf","r")
sp_fp = xrn.fopen("xstart_point.xf","r")
xm_fp = xrn.fopen("xrn_matrix.xm","rb")

if (( bk_fp == None ) or ( mk_fp == None ) or ( sp_fp == None ) or ( xm_fp == None ) ):
    exit( -1 )

if ( xrn.XSUCCESS != xrn.xrn_make_binomial_key_wrapper( xm_fp, sp_fp, mk_fp, bk_fp, settings ) ) :
    print("Failed to make a binomial key")
    xrn.fclose(xm_fp)
    xrn.fclose(mk_fp)
    xrn.fclose(sp_fp)
    xrn.fclose(bk_fp)
    exit( -1 )

xrn.fclose(xm_fp)
xrn.fclose(mk_fp)
xrn.fclose(sp_fp)
xrn.fclose(bk_fp)

#########################################
## Make xmonovalent key ( start, end ) ##
#########################################

print( "making monovalent and polyvalent key, and polyvalant proof..." )

# create a always valid key

start_timestamp = 0;
end_timestamp = 0xffffffffffffffff;

xm_fp     = xrn.fopen( "xrn_matrix.xm", "rb" );
pkey_fp   = xrn.fopen( "xpolyvalent_key.xf", "wb" );
pproof_fp = xrn.fopen( "xpolyvalent_proof.xf", "wb" );
mkey_fp   = xrn.fopen( "xmonovalent_key.xf", "wb" );

if ( xrn.XSUCCESS != xrn.xrn_make_monovalent_key_wrapper( xm_fp,        # the Xoron matrix file pointer
                                               start_timestamp,     # start of validity in unix time
                                               end_timestamp,       # end of validity in unix time 
                                               pkey_fp,   # the polyvalent key (secret key)
                                               pproof_fp, # the polyvalent proof (secret proof of having generated the monovalent key)
                                               mkey_fp,   # the monovalent key (key that can be disclosed)
                                               settings ) ) :
    xrn.fclose( xm_fp )
    xrn.fclose( pkey_fp )
    xrn.fclose( pproof_fp )
    xrn.fclose( mkey_fp )
    exit( -1 )

xrn.fclose( xm_fp )
xrn.fclose( pkey_fp )
xrn.fclose( pproof_fp )
xrn.fclose( mkey_fp )

'''
    /////////////////////////////////////////
    // Make xmonovalent key ( start, end ) //
    /////////////////////////////////////////

    printf( "making monovalent and polyvalent key, and polyvalant proof...\n" );

    // create a always valid key

    start_timestamp = 0;
    end_timestamp = 0xffffffffffffffff;

    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    polyvalent_key_fp = fopen( "xpolyvalent_key.xf", "wb" );
    polyvalent_proof_fp = fopen( "xpolyvalent_proof.xf", "wb" );
    monovalent_key_fp = fopen( "xmonovalent_key.xf", "wb" );

    if ( XSUCCESS != xrn_make_monovalent_key_wrapper( decompressed_xrn_matrix_fp,        // the Xoron matrix file pointer
                                                   start_timestamp,     // start of validity in unix time
                                                   end_timestamp,       // end of validity in unix time 
                                                   polyvalent_key_fp,   // the polyvalent key (secret key)
                                                   polyvalent_proof_fp, // the polyvalent proof (secret proof of having generated the monovalent key)
                                                   monovalent_key_fp,   // the monovalent key (key that can be disclosed)
                                                   settings ) ) {
        fclose( decompressed_xrn_matrix_fp );
        fclose( polyvalent_key_fp );
        fclose( polyvalent_proof_fp );
        fclose( monovalent_key_fp );
        return -1;
    }

    // An alternative to the xrn_make_monovalent_key_wrapper is the xrn_make_binomial_key function
    // if ( XSUCCESS !=
    //      xrn_make_monovalent_key_wrapper ( xrn_matrix, // the xrn_matrix is of the type ( xrn_matrix_t * )
    //                          xpolyvalent_key,  // the polyvalent key is of the type ( xpolyvalent_key_t )
    //                          xpolyvalent_proof, // the polyvalent proof is of the type ( xpolyvalent_proof_t )
    //                          xmonomial_key  // the xmonomial_key is of the type ( xmonovalent_key_t * )
    //                        ) ) {

    if ( ( 0 != fclose( polyvalent_key_fp ) ) || ( 0 != fclose( polyvalent_proof_fp ) )
         || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) || ( 0 != fclose( monovalent_key_fp ) ) ) {
        return -1;
    }

    printf( "keys successfully created\n" );
    return 0;
'''

exit( 0 )

